﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GPDAexplorer
{
    //文件内容读取封装
    class FileContent
    {
        FileStream f;
        public FileContent(string path)
        {
            f = File.Open(path, FileMode.Open, FileAccess.Read);
        }
        
        public byte[] FileHead()
        //读取文件头
        {
            byte[] ret = new byte[16];
            f.Seek(0, SeekOrigin.Begin);
            f.Read(ret, 0, 16);
            return ret;
            
        }
        
        public int ReadInteger(int offset, int len)//fdafdsfad
        //读取整型
        {
            int ret = 0;
            if (len > 4) throw new TooLargeIntegerLengthExpection();
            long pos = f.Position;
            f.Seek(offset, SeekOrigin.Begin);
            for (int i = offset + len - 1; i >= offset; i--)
            {
                ret <<= 8;
                ret += f.ReadByte();
            }
            f.Seek(pos, SeekOrigin.Begin);
            return ret;
        }
        
        public string ReadString(int offset, int len,Encoding en)
        //读取字符串
        {
            char[] ret;
            byte[] buf=new byte[1024];
            long pos = f.Position;
            f.Seek(offset, SeekOrigin.Begin);
            f.Read(buf, 0, len);
            ret = en.GetChars(buf,0,len);
            f.Seek(pos, SeekOrigin.Begin);
            return new String(ret);
        }
    }
}
